//
//  LGSendDoctorAdviceController.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/6/15.
//  开具医嘱

#import "LGSendDoctorAdviceController.h"
//发送成功
#import "LGAdviceSendSuccesController.h"
#import "LGAdviceMedicineController.h"
#import "LGAdviceOtherController.h"
#import "LGPickerViewMax.h"
@interface LGSendDoctorAdviceController ()<LGAdviceMedicineDelegate,LGAdviceOtherDelegate>
/**   */
@property (nonatomic , strong)LGNavigationView *navigationView;
/**   */
@property (nonatomic , strong)CAGradientLayer *gradientLayer;
/**   */
@property (nonatomic , strong)LGAdviceMedicineController *medicineVC;
/**   */
@property (nonatomic , strong)LGAdviceOtherController *otherVC;
/**   */
@property (nonatomic , strong)NSMutableArray *controllerArray;
/**   */
@property (nonatomic , strong)LGEmptyView *emptyView;
/**   */
@property (nonatomic , strong)UIButton *sendBtn;
/**   */
@property (nonatomic , strong)NSArray *titleArray;
/**   */
@property (nonatomic , strong)NSArray *widthArray;
/**   */
@property (nonatomic , strong)NSArray *doctorArray;

@end

@implementation LGSendDoctorAdviceController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.appointid = (_appointid && _appointid.length>0)?self.appointid:@"0";
    self.source = (_source && _source.length>0)?self.source:@"";
    [self commonInit];
    [self requestDoctorArray];
}
-(void)backButtonAction{
    if(self.medicineVC.hasEdit == YES || self.otherVC.hasEdit == YES){
        __weak typeof(self) weakSelf = self;
        [LGAlertTool alertWithTitle:@"放弃编辑" content:@"确定放弃编辑？" sureAction:^{
            if(self.medicineVC.adviceid && self.medicineVC.adviceid.length>0){
                [RequestUtil withPOST:@"/doctor/medical_advice.medical_advice/clear" parameters:@{@"advice_id":self.medicineVC.adviceid} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
                    
                } error:^(NSError *error) {
                    
                }];
            }
            [weakSelf.navigationController popViewControllerAnimated:YES];
        }];
    }else{
        if(self.medicineVC.adviceid && self.medicineVC.adviceid.length>0){
            [RequestUtil withPOST:@"/doctor/medical_advice.medical_advice/clear" parameters:@{@"advice_id":self.medicineVC.adviceid} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
                
            } error:^(NSError *error) {
                
            }];
        }
        [self.navigationController popViewControllerAnimated:YES];
    }
}
-(void)requestDoctorArray{
    [RequestUtil withPOST:@"/doctor/Select/teamDoctorOptions" parameters:@{} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
        if(code == 200){
            self.doctorArray = returnData;
        }
    } error:^(NSError *error) {
        
    }];
}

//发送医嘱
-(void)sendBtnAction{
    if(self.sendBtn.selected == NO) return;
    [self.otherVC saveDataRequest];
    __weak typeof(self) weakSelf = self;
    
    //护士、个案管理师发送前要选择医生
    if([kRoleId isEqualToString:@"20"] || [kRoleId isEqualToString:@"30"]){
        if(self.doctorArray.count == 0){
            [self requestDoctorArray];
        }else{
            NSMutableArray *pickerArray = [NSMutableArray array];
            for (NSInteger i=0; i<self.doctorArray.count; i++) {
                NSDictionary *tempDic = self.doctorArray[i];
                [pickerArray addObject:LGNSString(tempDic[@"title"])];
            }
            LGPickerViewMax *pickerView = [[LGPickerViewMax alloc]initWithFrame:CGRectMake(0, 0, Screen_W, Screen_H)];
            [pickerView.sureBtn setTitle:@"确定发送" forState:UIControlStateNormal];
            pickerView.titleLabel.text = @"本次开具医嘱的医生";
            pickerView.pickArry = @[pickerArray];
            [[UIApplication sharedApplication].keyWindow addSubview:pickerView];
            pickerView.selectPickViewIndex = ^(NSArray *indexArray) {
                NSInteger index = [indexArray[0] integerValue];
                NSDictionary *doctorDic = weakSelf.doctorArray[index];
                [RequestUtil withPOST:@"/doctor/medical_advice.medical_advice/send" parameters:@{@"patient_id":self.patientid,@"followup_id":self.followid,@"appointment_id":self.appointid,@"source":self.source,@"send_doctor_id":LGNSString(doctorDic[@"value"])} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
                    if(code == 200){
                        LGAdviceSendSuccesController *controller = [[LGAdviceSendSuccesController alloc]init];
                        controller.patientid = weakSelf.patientid;
                        [weakSelf.navigationController pushViewController:controller animated:YES];
                    }else{
                        [TooltipView showMessage:message offset:0];
                    }
                } error:^(NSError *error) {
                    [TooltipView showMessage:LGErrorMessage offset:0];
                }];
            };
        }
    }else{
        [LGAlertTool alertWithTitle:@"发送医嘱" content:@"确定发送给患者？" sureAction:^{
            [RequestUtil withPOST:@"/doctor/medical_advice.medical_advice/send" parameters:@{@"patient_id":self.patientid,@"followup_id":self.followid,@"appointment_id":self.appointid,@"source":self.source,@"send_doctor_id":kUserId} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
                if(code == 200){
                    LGAdviceSendSuccesController *controller = [[LGAdviceSendSuccesController alloc]init];
                    controller.patientid = weakSelf.patientid;
                    [weakSelf.navigationController pushViewController:controller animated:YES];
                }else{
                    [TooltipView showMessage:message offset:0];
                }
            } error:^(NSError *error) {
                [TooltipView showMessage:LGErrorMessage offset:0];
            }];
        }];
    }
}

-(void)observerSendStatus{
    if(self.medicineVC.dataArray.count == 0 && self.otherVC.content.length == 0 && self.otherVC.recordArray.count == 0){
        self.sendBtn.selected = NO;
    }else{
        self.sendBtn.selected = YES;
    }
}

#pragma mark
#pragma mark ====> WMPageControllerDelegate
- (NSInteger)numbersOfChildControllersInPageController:(WMPageController *)pageController {
    return self.titleArray.count;
}

- (NSString *)pageController:(WMPageController *)pageController titleAtIndex:(NSInteger)index {
    return self.titleArray[index];
}

- (UIViewController *)pageController:(WMPageController *)pageController viewControllerAtIndex:(NSInteger)index {
    return self.controllerArray[index];
}

- (CGFloat)menuView:(WMMenuView *)menu widthForItemAtIndex:(NSInteger)index {
    return Screen_W/2.0;
}

- (CGRect)pageController:(WMPageController *)pageController preferredFrameForMenuView:(WMMenuView *)menuView {
    return CGRectMake(0, topBarHeight, Screen_W, viewPix(44));
}

- (CGRect)pageController:(WMPageController *)pageController preferredFrameForContentView:(WMScrollView *)contentView {
    return CGRectMake(0, topBarHeight+viewPix(44), Screen_W, Screen_H-topBarHeight-viewPix(44));
}

- (void)pageController:(WMPageController *)pageController willEnterViewController:(__kindof UIViewController *)viewController withInfo:(NSDictionary *)info{
    NSLog(@">>>>>willEnterView>>>%@",viewController.title);
}

- (void)pageController:(WMPageController *)pageController didEnterViewController:(__kindof UIViewController *)viewController withInfo:(NSDictionary *)info{
    NSLog(@">>>>>didEnterView>>>%@",viewController.title);
}



-(void)commonInit{
    self.fd_prefersNavigationBarHidden = YES;
    self.view.backgroundColor = [UIColor colorWithString:@"#F2F3F5"];
    [self.view addSubview:self.navigationView];
    [self.navigationView addSubview:self.sendBtn];
    [self.view.layer addSublayer:self.gradientLayer];
    
    self.titleArray = @[@"用药方案",@"其他医嘱"];
    [self addChildViewController:self.medicineVC];
    [self addChildViewController:self.otherVC];
    [self.controllerArray addObject:self.medicineVC];
    [self.controllerArray addObject:self.otherVC];
    
    self.titleColorNormal = [UIColor colorWithString:@"#666666"];
    self.titleColorSelected = [UIColor colorWithString:@"#4C87FD"];
    self.progressColor = [UIColor colorWithString:@"#3291FF"];
    self.menuViewStyle=WMMenuViewStyleLine;
    self.titleFontName = @"STHeitiTC-Medium";
    self.titleSizeSelected = 16;
    self.titleSizeNormal = 14;
    self.progressWidth = viewPix(13);
    self.progressHeight = 4;
    self.progressViewBottomSpace = 0;
    self.progressViewCornerRadius = 2;
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.sendBtn];
    [self reloadData];
    self.scrollView.backgroundColor = [UIColor clearColor];
}

-(NSMutableArray *)controllerArray{
    if(!_controllerArray){
        _controllerArray = [NSMutableArray array];
    }
    return _controllerArray;
}


-(LGAdviceMedicineController *)medicineVC{
    if(!_medicineVC){
        _medicineVC = [[LGAdviceMedicineController alloc]init];
        _medicineVC.patientid = self.patientid;
        _medicineVC.followid = self.followid;
        _medicineVC.delegate = self;
    }
    return _medicineVC;
}

-(LGAdviceOtherController *)otherVC{
    if(!_otherVC){
        _otherVC = [[LGAdviceOtherController alloc] init];
        _otherVC.patientid = self.patientid;
        _otherVC.followid = self.followid;
        _otherVC.delegate = self;
    }
    return _otherVC;
}

-(UIButton *)sendBtn{
    if(!_sendBtn){
        _sendBtn = [UIButton buttonWithTitle:@"发送给患者" titleFont:LGFontWeight(14, UIFontWeightMedium) normalColor:@"#666666" selectColor:@"#429BFF"];
        _sendBtn.frame = CGRectMake(Screen_W-viewPix(90), statusBarHeight, viewPix(74), 44);
        _sendBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
        [_sendBtn addTarget:self action:@selector(sendBtnAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _sendBtn;
}
-(CAGradientLayer *)gradientLayer{
    if(!_gradientLayer){
        _gradientLayer = [CAGradientLayer layer];
        _gradientLayer.frame = CGRectMake(0,topBarHeight,Screen_W,viewPix(167));
        _gradientLayer.startPoint = CGPointMake(0.5, 0);
        _gradientLayer.endPoint = CGPointMake(0.5, 1.03);
        _gradientLayer.colors = @[(__bridge id)[UIColor colorWithString:@"#E5F3FF"].CGColor, (__bridge id)[UIColor colorWithString:@"#F3F9FE"].CGColor, (__bridge id)[UIColor colorWithString:@"#FEFEFF" alpha:0].CGColor];
        _gradientLayer.locations = @[@(0), @(0.4f), @(1.0f)];
    }
    return _gradientLayer;
}

-(LGNavigationView *)navigationView{
    if(!_navigationView){
        _navigationView = [[LGNavigationView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, topBarHeight)];
        _navigationView.backgroundColor = [UIColor colorWithString:@"#E5F3FF"];
        _navigationView.title = @"开具医嘱";
        [_navigationView.backBtn addTarget:self action:@selector(backButtonAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _navigationView;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
